package com.mbf.crm.common;

import org.springframework.web.socket.TextMessage;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Jackson可以轻松的将Java对象转换成json对象和xml文档，同样也可以将json、xml转换成Java对象。
 * @author 晏鼠  张劲松
 *
 */
public class SocketMessage {

	private String tag;
	private Object data;
	
	public SocketMessage() {
	}

	public SocketMessage(String tag, Object data) {
		super();
		this.tag = tag;
		this.data = data;
	}

	public String getTag() {
		return tag;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}

	public Object getObject() {
		return data;
	}

	public void setObject(Object data) {
		this.data = data;
	}
	
	@Override
	public String toString() {
		return "SocketMessage [tag=" + tag + ", data=" + data + "]";
	}

	/**
	 * 转换成TextMessage进行推送
	 * @return
	 */
	public TextMessage toTextMessage(){
		ObjectMapper mapper = new ObjectMapper();
		try {
			return new TextMessage(mapper.writeValueAsString(this));
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return null;
	}
	
}
